<?php
	require_once( 'core_DataBaseConnect.php' );
	require_once( '_configs.php' );
	
	class BusinessDAO
	{
		// 코드 중복 체크
		public static function isAvailableBusiness( DataBaseConnect $con, $business_code )
		{
			$query = 'SELECT COUNT(SEQ) FROM '.configs::TABLE_BUSINESS." WHERE `CODE`='$business_code';";
			$result = $con->query( $query );
			
			$row = DataBaseConnect::fetch_assoc($result);
			
			return ($row[ 'COUNT(SEQ)' ] == '0');
		}
		
		// 새 사업 추가
		public static function insertBusiness( DataBaseConnect $con, 
											$code, 
											$businessCode, 
											$name, 
											$pDate, 
											$dDate, 											
											$ref,
											$refLicense,
											$ownerID )
		{
			$query = 'INSERT INTO '.configs::TABLE_BUSINESS.
						"(
							`SEQ` ,
							`CODE` ,
							`B_CODE` ,
							`NAME` ,
							`PROPOSE_DATE`,
							`DECISION_DATE` ,
							`REF`,
							`REF_LICENSE`,
							`OWNER_ID`
						)
						VALUES 
						(
							NULL,
							'$code',
							'$businessCode',
							'$name',
							'$pDate',
							'$dDate',
							'$ref',
							'$refLicense',
							'$ownerID'
						);";
			return $con->query( $query );
		}
		
		// 추가 썸네일 경로 갱신
		public static function insertBusinessThumbs( DataBaseConnect $con, 
													$business_code, 
													$thumbnails )
		{
			foreach( $thumbnails as $k=>$v )
			{
				$query = 'INSERT INTO '.configs::TABLE_BUSINESSTHUMBS.
							"(
								`SEQ` ,
								`B_CODE` ,
								`LINK`
							)
							VALUES 
							(
								NULL,
								'$business_code',
								'$v'
							);";
				$con->query( $query );
			}
		}
		
		// 전체 ROW 수
		public static function countBusinesses( DataBaseConnect $con )
		{
			$query = 'SELECT COUNT(*) FROM '.configs::TABLE_BUSINESS.';';
			$fetch = DataBaseConnect::fetch_assoc( $con->query( $query ) );
			return $fetch[ 'COUNT(*)' ];
		}
		
		// 모든 목록 가져오기
		public static function selectBusinesses( DataBaseConnect $con,
												$page,
												$numPerPage )
		{
			$startIndex = max( ($page-1)*$numPerPage, 0 );
			$query = 'SELECT * FROM '.configs::TABLE_BUSINESS." LIMIT $startIndex,$numPerPage;";
			return $con->query( $query );
		}
		
		// 단일 항목 가져오기
		public static function selectBusiness( DataBaseConnect $con,
												$business_code )
		{
			$query = 'SELECT * FROM '.configs::TABLE_BUSINESS." WHERE `CODE`='$business_code' LIMIT 1;";
			return $con->query( $query );
		}
		
		// 사업에 설정된 모든 썸네일 정보를 가져옴
		public static function selectBusinessThumnails( DataBaseConnect $con,
														$business_code )
		{
			$query = 'SELECT * FROM '.configs::TABLE_BUSINESSTHUMBS." WHERE `B_CODE`='$business_code';";
			return $con->query( $query );
		}
		
		// 썸네일 이미지 경로
		public static function selectThumbPath( DataBaseConnect $con,
														$thumb_seq )
		{
			$query = 'SELECT LINK FROM '.configs::TABLE_BUSINESSTHUMBS." WHERE `SEQ`='$thumb_seq' LIMIT 1;";
			return $con->query( $query );
		}
		
		// 썸네일 정보를 삭제
		public static function deleteThumbPath( DataBaseConnect $con,
														$thumb_seq )
		{
			$query = 'DELETE FROM '.configs::TABLE_BUSINESSTHUMBS." WHERE `SEQ`='$thumb_seq' LIMIT 1;";
			return $con->query( $query );
		}
		
		
		// 사업 정보 업데이트
		public static function updateBusiness(	DataBaseConnect $con,
												$code, 
												$businessCode, 
												$name, 
												$pDate,
												$dDate,												
												$ref,
												$refLicense )
		{
			$query = 'UPDATE '.configs::TABLE_BUSINESS."
			 			SET
							`B_CODE`='$businessCode',
							`NAME`='$name',
							`PROPOSE_DATE`='$pDate',
							`DECISION_DATE`='$dDate',
							`REF`='$ref',
							`REF_LICENSE`='$refLicense'
						WHERE
							`CODE`='$code'
						LIMIT 1;";
			return $con->query( $query );
		}
		
		
		// 사업 링크 추가
		public static function insertBusinessLink(	DataBaseConnect $con,
												$code, 
												$link )
		{
			$query = 'INSERT INTO '.configs::TABLE_BUSINESSLINKS."
						(
							`B_CODE`,
							`LINK`
						)VALUES(
							'$code',
							'$link'
						);";
			return $con->query( $query );
		}
		
		// 사업 링크 조회
		public static function selectBusinessLinks( DataBaseConnect $con, $businessCode )
		{
			$query = 'SELECT * FROM '.configs::TABLE_BUSINESSLINKS." WHERE `B_CODE`='$businessCode';";
			return $con->query( $query );
		}
		
		// 사업 링크 삭제
		public static function deleteBusinessLink( DataBaseConnect $con, $seq )
		{
			$query = 'DELETE FROM '.configs::TABLE_BUSINESSLINKS." WHERE `SEQ`='$seq' LIMIT 1;";
			return $con->query( $query );
		}
		
		
		// 사업 태그 존재 여부
		public static function hasBusinessTag(	DataBaseConnect $con,
													$code, 
													$tag )
		{
			$query = 'SELECT COUNT(SEQ) FROM '.configs::TABLE_BUSINESSTAGS."
						WHERE
							`B_CODE`='$code'
							AND
							`TAG`='$tag';";
			$fetch = DataBaseConnect::fetch_assoc( $con->query( $query ) );
			return ( $fetch[ 'COUNT(SEQ)' ] != '0' );
		}
		
		// 사업 태그 추가
		public static function insertBusinessTag(	DataBaseConnect $con,
													$code, 
													$tag )
		{
			$query = 'INSERT INTO '.configs::TABLE_BUSINESSTAGS."
						(
							`B_CODE`,
							`TAG`
						)VALUES(
							'$code',
							'$tag'
						);";
			return $con->query( $query );
		}
		
		// 사업 태그 조회
		public static function selectBusinessTags( DataBaseConnect $con, $businessCode )
		{
			$query = 'SELECT * FROM '.configs::TABLE_BUSINESSTAGS." WHERE `B_CODE`='$businessCode';";
			return $con->query( $query );
		}
		
		// 사업 태그 삭제
		public static function deleteBusinessTag( DataBaseConnect $con, $seq )
		{
			$query = 'DELETE FROM '.configs::TABLE_BUSINESSTAGS." WHERE `SEQ`='$seq' LIMIT 1;";
			return $con->query( $query );
		}
		
		// 모든 태그 목록 조회
		public static function selectAllBusinessTags( DataBaseConnect $con )
		{
			$query = 'SELECT Distinct`TAG` FROM '.configs::TABLE_BUSINESSTAGS.';';
			return $con->query( $query );
		}
	}
?>